<?php
use think\facade\Db;
class userLogin{
    public function TODO($ws,$frame,$fd){ 
        $param = (array)(json_decode($frame->data));  
        
       
        $isEmpty = Db::name('user')->where('username',$param['username'])->find();
        
        if($isEmpty && md5($param['passwd']) == $isEmpty['pass']){
            if(!empty($isEmpty['fd'])){
                $ws->push($isEmpty['fd'],json_encode(['status'=>1,'msg'=>'您的账号已在其他设备登陆','act'=>'loginOut']));
            }
            $userData = [ 
                'expireTime'   => time()+10*60*60, 
                'fd'        =>  $fd,
            ];
            $result = Db::name('user')->where("username",$param['username'])->update($userData); 
        
            $userFriend =  Db::name('friend')->where("memberTo",$param['username'])->order('fd desc')->select()->toArray();   
            foreach($userFriend as $k =>$v){  
                $userFriend[$k]['msgCount'] =   Db::name('chatlog')
                                                ->where("toMember",$param['username'])
                                                ->where("memberTo",$v['toMember'])
                                                ->where("isRead",0)
                                                ->count();
            }   
        if($isEmpty){
                return [
                    'status'=>1,
                    'msg'=>'登录成功',
                    'data'=>['fd'=>$fd,'expireTime'=>time()+10*60*60,'uuid'=>$isEmpty['uuid'],'username'=>$param['username'],'allFriend'=>$userFriend], 
                    'act'=>'userLogin',
                    'fd'=>$fd,
                ];
            }else{
                return [
                    'status'=>0,
                    'msg'=>'账号或密码错误!',
                    'data'=>'', 
                ];
            }
        }else{
            return [
                'status'=>0,
                'msg'=>'账号或密码错误',
                'data'=>'', 
            ];
        }
    }
}